package org.youyun56.gpsserver.db;

import java.net.SocketAddress;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.youyun56.gpsserver.common.DateUtils;
import org.youyun56.gpsserver.entity.Car;

import com.j3.sql.BatchFunc;
import com.j3.sql.GridUtils;
import com.j3.sql.SqlFunc;

public class CarDao {

    private final static String findCarByCarNamePwd = "select id,province,carId,carMobile,pwd,city_id,lastPositionTime,carryStatus,position,partner,imsi from car where (carId = ? or carMobile = ?) and pwd = ?";

    public Car findCar(String carId, String pwd) throws SQLException {
        GridUtils grid = new GridUtils();
        grid.init(findCarByCarNamePwd, new Object[] { carId, carId, pwd }, new int[] { Types.VARCHAR, Types.VARCHAR,
                Types.VARCHAR });
        if (grid.next()) {
            Car car = new Car();
            car.setId(grid.getLong("id"));
            car.setCarId(grid.get("carId"));
            car.setCarMobile(grid.get("carMobile"));
            car.setPwd(grid.get("pwd"));
            car.setPartner(grid.getInt("partner"));
            car.setProvince(grid.getInt("province"));
            car.setCityId(grid.getInt("city_id"));
            car.setLastPositionTime(grid.getDateTimeString("lastPositionTime"));
            car.setCarryStatus(grid.get("carryStatus"));
            car.setPosition(grid.get("position"));
            car.setImsi(grid.get("imsi"));
            car.setFocusCity(car.getCityId());
            return car;
        }
        return null;
    }

    private final static String updateCarById = "select lastPositionTime,carryStatus,position from car where id = ?";

    public Car updateCar(Car car) throws SQLException {
        GridUtils grid = new GridUtils();
        grid.init(updateCarById, new Object[] { car.getId() }, new int[] { Types.BIGINT });
        if (grid.next()) {
            car.setLastPositionTime(grid.getDateTimeString("lastPositionTime"));
            car.setCarryStatus(grid.get("carryStatus"));
            car.setPosition(grid.get("position"));
        }
        return car;
    }

    public void updateCarLoginInfo(SocketAddress sa, Long id, String version, String imsi) throws SQLException {
        List<String> sqls = new ArrayList<String>();
        sqls.add(String.format("update car set car.cversion = '%s',lastAccessTime = '%s',imsi = '%s' where id = %d",
                version, DateUtils.getDateTime(), imsi, id));
        sqls.add(String.format("insert into carlogin(carid,ip,accessTime) values(%d,'%s','%s')", id, sa.toString(),
                DateUtils.getDateTime()));
        BatchFunc fun = new BatchFunc();
        fun.doBatch(sqls);
    }

    public void updateCarGpsLocation(Long id, String lnt, String lat) throws SQLException {
        SqlFunc fun = new SqlFunc();
        fun.executeUpdate(String.format("update car set car.x = '%s',y = '%s',lastPositionTime = NOW() where id = %d",
                lnt, lat, id));
    }

    private final static String findCarById = "select id,province,carId,carMobile,pwd,city_id,lastPositionTime,carryStatus,position,partner,imsi from car where id = ?";

    public Car findCarById(Integer carid) throws SQLException {
        GridUtils grid = new GridUtils();
        grid.init(findCarById, new Object[] { carid }, new int[] { Types.BIGINT });
        if (grid.next()) {
            Car car = new Car();
            car.setId(grid.getLong("id"));
            car.setCarId(grid.get("carId"));
            car.setCarMobile(grid.get("carMobile"));
            car.setPwd(grid.get("pwd"));
            car.setPartner(grid.getInt("partner"));
            car.setProvince(grid.getInt("province"));
            car.setCityId(grid.getInt("city_id"));
            car.setLastPositionTime(grid.getDateTimeString("lastPositionTime"));
            car.setCarryStatus(grid.get("carryStatus"));
            car.setPosition(grid.get("position"));
            car.setImsi(grid.get("imsi"));
            car.setFocusCity(car.getCityId());
            return car;
        }
        return null;
    }

}
